package com.csweimei.rentmanagementtreasure.enterprise.viewModel;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.chad.library.BR;
import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.http.entity.CustomerCenterBean;
import com.csweimei.rentmanagementtreasure.pop.PopBean;
import com.csweimei.rentmanagementtreasure.pop.PopChooseXq;
import com.csweimei.rentmanagementtreasure.pop.PopSelClick;
import com.csweimei.rentmanagementtreasure.pop.PopUtils;
import com.csweimei.rentmanagementtreasure.pop.PopWheelBase;
import com.csweimei.rentmanagementtreasure.viewModel.TitleVM;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class CustomerCenterVM extends TitleVM {
    public CustomerCenterVM(@NonNull Application application, BaseModel model) {
        super(application, model);
        setTitleText("客户中心");
    }


    public ObservableField<String> companyid = new ObservableField<>();
    public ObservableField<String> keyword = new ObservableField<>("");
    public ObservableField<String> px = new ObservableField<>("最近录入");
    public ObservableField<String> strRent = new ObservableField<>("");
    public ObservableField<String> endRent = new ObservableField<>("");
    public ObservableField<String> rent = new ObservableField<>("");
    public ObservableField<String> HouseType = new ObservableField<>("");
    public ObservableField<String> minArea = new ObservableField<>("");
    public ObservableField<String> maxArea = new ObservableField<>("");
    public ObservableField<String> area = new ObservableField<>("");
    public ObservableField<String> toward = new ObservableField<>("");
    public ObservableField<String> other = new ObservableField<>("");
    public ObservableField<String> customType = new ObservableField<>("全部");


    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    public ObservableList<CCItemPVM> observableList = new ObservableArrayList<>();
    public ItemBinding<CCItemPVM> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_customer_ent);

    public ObservableField<Integer> showNo = new ObservableField<>();
    private void getData() {
        Map<String,String> map = new HashMap<>();
        map.put("companyid",companyid.get());
        map.put("keyword",keyword.get());
        map.put("px",px.get());
        map.put("strRent",strRent.get());
        map.put("endRent",endRent.get());
        map.put("HouseType",HouseType.get());
        map.put("minArea",minArea.get());
        map.put("maxArea",maxArea.get());
        map.put("toward",toward.get());
        map.put("other",other.get());
        map.put("customType",customType.get());
        model.customcenter(map)
                .compose(RxUtils.exceptionTransformer())
                .compose(RxUtils.schedulersTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<CustomerCenterBean>() {
                    @Override
                    public void onNext(CustomerCenterBean o) {
                        if (o.issucc){
                            observableList.clear();
                            if (o.getData().size() > 0){
                                showNo.set(View.GONE);
                                for (CustomerCenterBean.DataDTO bean : o.getData()) {
                                    CCItemPVM itemPVM = new CCItemPVM(CustomerCenterVM.this);
                                    itemPVM.setData(bean);
                                    observableList.add(itemPVM);

                                }
                            }else {
                                showNo.set(View.VISIBLE);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        showNo.set(View.VISIBLE);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public BindingCommand showPx = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            PopWheelBase popWheelBase = new PopWheelBase(mActivity, 1, new PopWheelBase.Click() {
                @Override
                public void click(String s) {
                    px.set(s);
                    onResume();
                }
            });

            popWheelBase.setList1(Arrays.asList(mActivity.getResources().getStringArray(R.array.customer_type1)));
            PopUtils.showPop(mActivity,popWheelBase,false);
        }
    });

    public BindingCommand showXq = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            PopChooseXq popChooseXq = new PopChooseXq(mActivity, (ysT, hxT, areaT, cxT, otherT) -> {
                rent.set(ysT);
                area.set(areaT);
                HouseType.set(hxT);
                toward.set(cxT);
                other.set(otherT);
                if (ysT.contains("-")){
                    String[] ys = ysT.split("-");
                    strRent.set(ys[0]);
                    endRent.set(ys[1].replace("元",""));
                }else {
                    strRent.set("不限");
                    endRent.set("不限");
                }


                if (areaT.contains("-")){
                    String[] area = areaT.split("-");
                    minArea.set(area[0]);
                    maxArea.set(area[1].replace("㎡",""));
                }else {
                    minArea.set("不限");
                    minArea.set("不限");
                }

                onResume();
            });
            PopUtils.showPop(mActivity,popChooseXq,false);
            popChooseXq.setChoose(rent.get(),HouseType.get(),area.get(),toward.get(),other.get());

        }
    });

    public BindingCommand showMore = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            PopWheelBase popWheelBase = new PopWheelBase(mActivity, 1, new PopWheelBase.Click() {
                @Override
                public void click(String s) {
                    customType.set(s);
                    onResume();
                }
            });

            popWheelBase.setList1(Arrays.asList(mActivity.getResources().getStringArray(R.array.customer_type3)));
            PopUtils.showPop(mActivity,popWheelBase,false);

        }
    });



}
